package secondriver.heathfood.util;

import android.util.Log;

/**
 * Author : secondriver
 * Date :  2015/10/21
 */
public class LogUtil {

    public enum Level {

        VERBOSE(1), DEBUG(2), INFO(3), WARN(4), ERROR(5), NOTHING(6);

        private final int value;

        Level(int value) {
            this.value = value;
        }

        public final int getValue() {
            return value;
        }
    }

    //LogUtil default log level is verbose
    private static Level usedLogLevel = LogUtil.usedLogLevel.VERBOSE;


    private static boolean isLogLevelSet = false;

    /**
     * This method only once call in application.
     *
     * @param level LogLevel
     * @see LogUtil.Level
     */
    public static void initLogLevel(Level level) {
        if (isLogLevelSet) {
            throw new RuntimeException("LogUtil log level already setting, don't set again.");
        } else {
            synchronized (usedLogLevel) {
                usedLogLevel = level;
                isLogLevelSet = true;
            }
        }
    }

    private String tag;

    private LogUtil(String tag) {
        this.tag = tag;
    }

    public static LogUtil getLogger(String tag) {
        return new LogUtil(tag);
    }

    public void verbose(String msg) {
        v(tag, msg);
    }

    public void verbose(String msg, Throwable t) {
        v(tag, msg, t);
    }

    public void verbose(String format, Object... args) {
        verbose(String.format(format, args));
    }

    public void verbose(Throwable t, String format, Object... args) {
        verbose(String.format(format, args), t);
    }

    public void debug(String msg) {
        d(tag, msg);
    }

    public void debug(String msg, Throwable t) {
        d(tag, msg, t);
    }


    public void debug(String format, Object... args) {
        debug(String.format(format, args));
    }

    public void debug(Throwable t, String format, Object... args) {
        debug(String.format(format, args), t);
    }

    public void info(String msg) {
        i(tag, msg);
    }

    public void info(String msg, Throwable t) {
        i(tag, msg, t);
    }

    public void info(String format, Object... args) {
        info(String.format(format, args));
    }

    public void info(Throwable t, String format, Object... args) {
        info(String.format(format, args), t);
    }


    public void warn(String msg) {
        w(tag, msg);
    }

    public void warn(String msg, Throwable t) {
        w(tag, msg, t);
    }

    public void warn(String format, Object... args) {
        warn(String.format(format, args));
    }

    public void warn(Throwable t, String format, Object... args) {
        warn(String.format(format, args), t);
    }

    public void error(String msg) {
        e(tag, msg);
    }

    public void error(String msg, Throwable t) {
        e(tag, msg, t);
    }

    public void error(String format, Object... args) {
        error(String.format(format, args));
    }

    public void error(Throwable t, String format, Object... args) {
        error(String.format(format, args), t);
    }


    public static void v(String tag, String msg) {
        if (usedLogLevel.getValue() <= usedLogLevel.VERBOSE.getValue()) {
            Log.v(tag, msg);
        }
    }

    public static void v(String tag, String msg, Throwable t) {
        if (usedLogLevel.getValue() <= usedLogLevel.VERBOSE.getValue()) {
            Log.v(tag, msg, t);
        }
    }

    public static void v(String tag, String format, Object... args) {
        v(tag, String.format(format, args));
    }

    public static void v(String tag, Throwable t, String format, Object... args) {
        v(tag, String.format(format, args), t);
    }

    public static void d(String tag, String msg) {
        if (usedLogLevel.getValue() <= usedLogLevel.DEBUG.getValue()) {
            Log.d(tag, msg);
        }
    }

    public static void d(String tag, String msg, Throwable t) {
        if (usedLogLevel.getValue() <= usedLogLevel.DEBUG.getValue()) {
            Log.d(tag, msg, t);
        }
    }

    public static void d(String tag, String format, Object... args) {
        d(tag, String.format(format, args));
    }

    public static void d(String tag, Throwable t, String format, Object... args) {
        d(tag, String.format(format, args), t);
    }

    public static void i(String tag, String msg) {
        if (usedLogLevel.getValue() <= usedLogLevel.INFO.getValue()) {
            Log.i(tag, msg);
        }
    }

    public static void i(String tag, String msg, Throwable t) {
        if (usedLogLevel.getValue() <= usedLogLevel.INFO.getValue()) {
            Log.i(tag, msg, t);
        }
    }

    public static void i(String tag, String format, Object... args) {
        i(tag, String.format(format, args));
    }

    public static void i(String tag, Throwable t, String format, Object... args) {
        i(tag, String.format(format, args), t);
    }

    public static void w(String tag, String msg) {
        if (usedLogLevel.getValue() <= usedLogLevel.WARN.getValue()) {
            Log.w(tag, msg);
        }
    }

    public static void w(String tag, String msg, Throwable t) {
        if (usedLogLevel.getValue() <= usedLogLevel.WARN.getValue()) {
            Log.w(tag, msg, t);
        }
    }

    public static void w(String tag, String format, Object... args) {
        w(tag, String.format(format, args));
    }

    public static void w(String tag, Throwable t, String format, Object... args) {
        w(tag, String.format(format, args), t);
    }

    public static void e(String tag, String msg) {
        if (usedLogLevel.getValue() <= usedLogLevel.ERROR.getValue()) {
            Log.e(tag, msg);
        }
    }

    public static void e(String tag, String msg, Throwable t) {
        if (usedLogLevel.getValue() <= usedLogLevel.ERROR.getValue()) {
            Log.e(tag, msg, t);
        }
    }

    public static void e(String tag, String format, Object... args) {
        e(tag, String.format(format, args));
    }

    public static void e(String tag, Throwable t, String format, Object... args) {
        e(tag, String.format(format, args), t);
    }

}
